const ArticleModel = require('../models/article')
class ArticleController{
  /**
   * 创建文章
   * @param ctx
   * @returns {Promise.<void>}
   */

   static async create(ctx){
     let req = ctx.request.body;
     if(req.title&&req.author&&req.content&&req.category){
       try {
         const ret = await ArticleModel.createArticle(req);
         const data = await ArticleModel.getArticleDetail(ret.id);
         ctx.response.status = 200
         ctx.body = {
           code: 200,
           msg: '创建成功',
           data
         }
       } catch (error) {
         ctx.response.status = 412
         ctx.body = {
           code: 412,
           msg: '创建失败',
           data: error
         }
       }
     } else {
       ctx.response.status = 416
       ctx.body = {
         code: 416,
         msg: '参数不齐'
       }
     }
   }

   /**
    * 获取文章详情
    * @param ctx
    * @returns {Promise.<void>}
    */
   static async detail(ctx) {
     let id = ctx.params.id

     if(id){
       try {
         let data = await ArticleModel.getArticleDetail(id);
         ctx.response.status = 200
         ctx.body = {
           code: 200,
           msg: '查询成功',
           data: data
         }
       } catch (error) {
         ctx.response.status = 412
         ctx.body = {
           code: 412,
           msg: '查询失败',
           data: data
         }
       }
     } else {
       ctx.response.status = 416
       ctx.body = {
         code: 416,
         msg: '未传文章id'
       }
     }
   }
}

module.exports = ArticleController